package cn.edu.zafu.bigdata.lyq.work9;
import java.io.*;
import java.net.*;
import java.util.*;
public class UDPServer {
    DatagramSocket socket = null;
    BufferedReader in = null;
    boolean moreQuotes = true;
    public void serverWork() throws IOException{
        socket = new DatagramSocket(12345);
        System.out.println("时钟开始同步，等待连接中......");
        byte[] buf = new byte[1024];
        while (moreQuotes){

            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            System.out.println("收到客户端请求");
            String currentTime = new Date().toString();
            byte[] time =currentTime.getBytes();
            InetAddress address = packet.getAddress();
            int port = packet.getPort();
            packet = new DatagramPacket(time, time.length,address,port);
            System.out.println(currentTime);
            socket.send(packet);

        }
        socket.close();
    }

    public static void main(String[] args) {
        UDPServer server = new UDPServer();
        try{
            server.serverWork();

        }catch (IOException e){e.printStackTrace();
        }
    }
}